<?php
/**
 * 修改分享的提交页面
 * 
 * @author zhangchao
 * @since 2012-05-15
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;

	//check refer  -  add by xinwei
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	cgi::both($g_cgival['album_id'], 'album_id', 0);
	$g_cgival['album_id'] = intval($g_cgival['album_id']);
	if($g_cgival['album_id'] <= 0)
	{
		throw new Exception('album_id不合法');
	}
	
	cgi::both($g_cgival["tag_name"], "tag_name", "");
	$g_cgival['tag_name'] = string::un_html(string::un_script_code(mod_status_core::sbc_to_dbc($g_cgival['tag_name'])));
	
	$tag_list = array();
	$tag = explode(" ",trim($g_cgival["tag_name"]));
	for ($i=0; $i<count($tag); $i++)
	{
		if (!empty($tag[$i]))
		{
			$tag_list[] = $tag[$i];
			if (!preg_match("/^([\x{4e00}-\x{9fa5}0-9A-Za-z_\-]+)$/u",$tag[$i]))
			{
					throw new Exception('1002:微博标签仅支持中英文、数字、"_"或减号', 0);
			}
			if (string_utf8::string_len($tag[$i])>7)
			{
				throw new Exception("标签内容超过7个字");
				break;
			}
		}
	}
	//删除重复
	$tag_list = array_unique($tag_list);

	if (count($tag_list)>5)
	{
		throw new Exception("最多添加5个标签");
	}
	$g_cgival["tag_name"] = $tag_list;
	
	cgi::both($g_cgival["album_content"], "album_content", "");
	$g_cgival["album_content"] = mod_status_core::sbc_to_dbc($g_cgival["album_content"]);
	$g_cgival["album_content"] = trim(preg_replace('/\s+/', ' ', $g_cgival["album_content"]));
	
	cgi::both($g_cgival["album_class"], "album_class", 299);
	$g_cgival["album_class"] = intval($g_cgival["album_class"]);
	
	
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();
	
	$db_food_album = new db_food_album();
	$data = $db_food_album->admin_get_food_album_by_ids(array($g_cgival['album_id']));
	if(empty($data[0]))
	{
		throw new Exception('该专辑不存在或已被删除');
	}
	else
	{
		$album_info = $data[0];
		$result0 = $db_food_album->admin_update_food_album_by_album_id($site_id, $album_info['album_id'], $album_info['album_title'], $g_cgival['album_class'], $g_cgival["album_content"]);
		
		$db_food_album_tag = new db_food_album_tag();
		$ori_tags_tmp = $db_food_album_tag->admin_get_tags_by_album_id($g_cgival['album_id']);
		$ori_tags = array();
		foreach ($ori_tags_tmp as $value) {
			$ori_tags[] = $value['tag'];
		}
		
		$tag_to_delete = array();
		$tag_to_insert = array();
		$tag_to_delete = array_diff($ori_tags, $g_cgival["tag_name"]);// 该删的tag
		$tag_to_insert = array_diff($g_cgival["tag_name"], $ori_tags);// 该增的tag

		foreach ($tag_to_delete as $value) {
			$db_food_album_tag->admin_delete_tag($g_cgival['album_id'], $value);
		}
		foreach ($tag_to_insert as $value) {
			$db_food_album_tag->admin_insert_tag($site_id, $g_cgival['album_id'], $value);
		}
	}
	
	$admin_log = new admin_log();
	$admin_log->admin_insert_log_info($admin_id, $site_id, 3, "编辑专辑 album_id={$g_cgival['album_id']}");
}

try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_cover_template_add_post.php");
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = '';
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	echo output::json('json',$g_show);
}

?>